package es.ats.codington.festival.mappers;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.springframework.jdbc.core.RowMapper;


import es.ats.codington.festival.domain.Imagen;

/**
 * 
 * @author Codington_RED
 *
 */

public class ImageMapper implements RowMapper<Imagen>{

	
	/**
	 * Mapper from an image
	 * @param resultSet
	 * @param numRow
	 * @return image
	 * @throws SQLException
	 */
	@Override
	public Imagen mapRow(ResultSet resultSet, int numRow) throws SQLException {
		Imagen image = new Imagen();
		
		
		image.setImageID(resultSet.getInt("idImage"));
		Blob blob = resultSet.getBlob("image");
        String name = resultSet.getObject("name").toString();
        byte[] data = blob.getBytes(1, (int)blob.length());
        BufferedImage img = null;
        try {
             img = ImageIO.read(new ByteArrayInputStream(data));
        } catch (IOException ex) {
          
        }
         
        image.setImage(img);
        image.setName(name);
         
		return image;
	}

}
